package com.sise.dubbo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sise.dubbo.common.PaginationRespBO;
import com.sise.dubbo.common.ServiceException;
import com.sise.dubbo.dao.TestUserDao;
import com.sise.dubbo.model.User;
import com.sise.dubbo.service.TestUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author hao_lin
 * @data 2018/10/9 17:36
 */
@Service
public class TestUserServiceImpl implements TestUserService {

    @Autowired
    private TestUserDao testUserDao;

    @Override
    public String printStr(String str) {
        return str + "hahahaha";
    }

    @Override
    public PaginationRespBO<User> findAll(int page) throws ServiceException {
        try {
            PageHelper.startPage(page,4);
            List<User> userList = testUserDao.findAll();
            PageInfo pageInfo=new PageInfo(userList);
            userListHandle(userList);
            PaginationRespBO<User> paginationRespBO=new PaginationRespBO(userList,pageInfo.getTotal());
            return paginationRespBO;
        } catch (ServiceException e) {
            throw new ServiceException("异常测试");
        }
    }

    @Override
    public PaginationRespBO<User> findByUsername(String username,int page) throws ServiceException {
        PageHelper.startPage(page,4);
        List<User> userList = testUserDao.findByUsername(username);
        PageInfo pageInfo=new PageInfo(userList);
        userListHandle(userList);
        PaginationRespBO<User> paginationRespBO=new PaginationRespBO<User>(userList,pageInfo.getTotal());
        return paginationRespBO;
    }


    private void userListHandle(List<User> userList) throws ServiceException {
        System.out.println("this is a handle");
    }
}
